//
//  DescrViewController.m
//  InTheSnow
//
//  Created by di on 7/23/09.
//  Copyright 2009 INTELLECTSOFT. All rights reserved.
//
#import "TopBarView.h"
#import "News.h"
#import "DescrViewController.h"
#import "UIBannerImageView.h"

@implementation DescrViewController

@synthesize currentNew;
@synthesize copyright;
@synthesize imgUrl;
@synthesize dataImg;
@synthesize newsNum;
@synthesize date;
@synthesize scroll;
@synthesize banner;
@synthesize titleNews;
@synthesize bigImage;
@synthesize description;
 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil news:(News*)new_ url:(NSURL*) url; {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.currentNew = new_;
		self.bigImage = nil;
		self.imgUrl = url;
		queueOper = [[NSOperationQueue alloc] init];
		
    }
    return self;
}

- (void)loadImgs {
	
	NSAutoreleasePool	 *autoreleasepool = [[NSAutoreleasePool alloc] init];	 
	
	NSData *bImgData = [[NSData alloc] initWithContentsOfURL:self.imgUrl]; 
	self.dataImg = bImgData; 	
	[bImgData release]; 	
	
	[self performSelectorOnMainThread:@selector(setImg) withObject:nil waitUntilDone:YES];
	[autoreleasepool release];
}

- (void)setImg{
	@synchronized(self.bigImage)
	{ 
		 
		if(self.dataImg != nil) 
		{
			UIImage *i = [[UIImage alloc] initWithData:self.dataImg]; 
			UIImageView *bigImaga = [[UIImageView alloc] initWithImage:i];
			[self setBigImage:bigImaga];
			[i release];
			[bigImaga release];
		}
		else{
			UIImageView *bigImaga = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@".png"]];
			[self.bigImage setFrame:CGRectMake(25, bigImaga.frame.origin.y, 0.0, 0.0)];
			[bigImaga release];
		}
		
		if(bigImage != nil)
		[bigImage setCenter:CGPointMake(160, titleNews.frame.origin.y+titleNews.frame.size.height+60)]; 
		if([__delegate isOffline])
		{
			[self.date setCenter:CGPointMake(160, titleNews.frame.origin.y + titleNews.contentSize.height)];
		 
			
			[self.description setFrame:CGRectMake(20, date.frame.origin.y+date.frame.size.height, 270, /*(descriptionSize.width / 260.0 +1.0) * 15.0 + 200*/description.contentSize.height)];
		}else
		{
			[date setCenter:CGPointMake(160, bigImage.frame.origin.y + bigImage.frame.size.height + 10)];
			[self.description setFrame:CGRectMake(20, bigImage.frame.origin.y+bigImage.frame.size.height + 5, 270, /*(descriptionSize.width / 260.0 +1.0) * 15.0 + 200*/description.contentSize.height )];
		}	 
		[self.scroll setContentSize:CGSizeMake(320, description.frame.origin.y + description.frame.size.height + 40)];
		
		[self.copyright setCenter:CGPointMake(160, description.frame.origin.y + description.frame.size.height)];
		
		[self.scroll addSubview:self.bigImage];	
	}
	
	[self.scroll setFrame:CGRectMake(0, 5, 320, 360)];
}

- (void)viewDidLoad { 
    [super viewDidLoad]; 
	
	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
		
	News *news = currentNew;//[[__delegate rssContentEvents] objectAtIndex:newsNum];
	news.title = news.title;		 
			
	self.titleNews.font = [UIFont boldSystemFontOfSize:17];
	self.titleNews.text = news.title;  
	self.titleNews.frame = CGRectMake(30, 96, 270, titleNews.contentSize.height);  
	
	[self.date setText:[news.date description]];
	[self.date setCenter:CGPointMake(160, titleNews.frame.origin.y + titleNews.contentSize.height + 10)];
	
	self.description.font = [UIFont fontWithName:@"Helvetica" size:14];
	self.description.text = news.description;	 	
	[self.description setFrame:CGRectMake(20, titleNews.frame.origin.y + titleNews.contentSize.height + 20 , 270,  description.contentSize.height)];	
	
	[self.scroll setContentSize:CGSizeMake(320, description.frame.origin.y + description.frame.size.height + 50)];	
	
	[self.copyright setCenter:CGPointMake(160, description.frame.origin.y + description.frame.size.height)];
	
	if(![__delegate isOffline])
	{
		loadOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImgs) object:nil];
		[queueOper addOperation:loadOperation]; 
	}
	if(__delegate.isOffline) { 
		self.titleNews.frame = CGRectMake(30, 10, 270,  titleNews.contentSize.height);  
		[self.description setFrame:CGRectMake(20, titleNews.frame.origin.y + titleNews.frame.size.height, 270,   description.contentSize.height)];
		[self.date setCenter:CGPointMake(160, titleNews.frame.origin.y + titleNews.contentSize.height)];
		[self.scroll setContentSize:CGSizeMake(320, description.frame.origin.y + description.frame.size.height + 50)];
		[self.copyright setCenter:CGPointMake(160, description.frame.origin.y + description.frame.size.height)];
		
		return;
	}
	if([[__delegate banners] count] < 1) 
	{
		self.banner = nil;
		NSArray *sv = [scroll subviews];
		for (UIView *v in sv) {
			[v setCenter:CGPointMake(v.center.x, v.center.y - 100)];
		}
		NSLog(@"banner count < 1"); 
		return;
	}
	UIBannerImageView *bann = (UIBannerImageView*)[[__delegate banners] objectAtIndex:0]; 
	[self.banner setUrl:bann.url];
	[self.banner  setAnimationImages:bann.animationImages];
	[self.banner setAnimationDuration:bann.timeout * [[bann animationImages] count]]; 
	[banner startAnimating];
}

- (void)backView{
	[self.navigationController popToRootViewControllerAnimated:YES];	
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated]; 
	
	[self.tabBarController.tabBar setHidden:NO];
	[[__delegate topBar] toBack].hidden = YES;
	[[__delegate topBar] toShare].hidden = YES;
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated]; 	
//	[self.scroll setFrame:CGRectMake(0, 40, 320, 436)];
	 [self.scroll setFrame:CGRectMake(0, 5, 320, 360)];
//	[self.tabBarController.tabBar setHidden:YES];
	[[__delegate topBar] toBack].hidden = NO;
	[[__delegate topBar] toShare].hidden = NO;
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backView) name:@"Back" object:nil];
	
	[__delegate.toolBar setHidden:YES];

}

- (void)viewDidAppear:(BOOL)animated {
 
    [super viewDidAppear:animated];
		[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
	[self.banner   setUserInteractionEnabled:YES];
}

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	if([loadOperation isExecuting]) [loadOperation cancel];
	[copyright release];
	[loadOperation release];
	[queueOper release];
	NSLog(@"dealloc description");
	[imgUrl release];
	[date release];
	[scroll release];
	[dataImg release];
	[description release];
	[bigImage release];
	[titleNews release];
	[banner release];
    [super dealloc];
}


@end
